local skel = fk.CreateSkill {
  name = "emo__chuoxu",
}

Fk:loadTranslationTable{
  ["emo__chuoxu"] = "踔虚",
  [":emo__chuoxu"] = "摸牌阶段，你可以选择任意项：1.将一种颜色所有手牌当【杀】使用；2.本回合手牌上限为2；3.将本回合弃牌阶段改为本阶段后执行。然后本阶段摸牌数增加已选项的序号数之和。",

  ["@@emo__chuoxu-turn"] = "踔虚",
  ["#emo__chuoxu-choice"] = "踔虚：选择任意项，令摸牌数增加所选项的序号数",
  ["emo__chuoxu_slash"] = "1:将一种颜色所有手牌当【杀】使用",
  ["emo__chuoxu_maxcard"] = "2:本回合手牌上限为2",
  ["emo__chuoxu_discard"] = "3:将本回合弃牌阶段改为此阶段后执行",
  ["#emo__chuoxu-slash"] = "踔虚：请将所有手牌当【杀】使用",
  ["emo__chuoxu_viewas"] = "踔虚",
}

skel:addEffect(fk.DrawNCards, {
  anim_type = "drawcard",
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local all_choices = {"emo__chuoxu_slash", "emo__chuoxu_maxcard", "emo__chuoxu_discard"}
    local choices = table.simpleClone(all_choices)
    if player:isKongcheng() then table.remove(choices, 1) end
    local chosen = room:askToChoices(player, {
      all_choices = all_choices, choices = choices, skill_name = skel.name, min_num = 1, max_num = 3, cancelable = true,
      prompt = "#emo__chuoxu-choice",
    })
    if #chosen == 0 then return false end
    chosen = table.map(chosen, function (ch) return table.indexOf(all_choices, ch) end)
    local cost_data = {}
    if table.contains(chosen, 1) then
      local _, dat = room:askToUseActiveSkill(player, {
        skill_name = "emo__chuoxu_viewas", prompt = "#emo__chuoxu-slash",
      })
      if dat then
        cost_data.slash_data = {color = dat.interaction, tos = dat.targets }
      else
        table.removeOne(chosen, 1)
        if #chosen == 0 then return false end
      end
    end
    cost_data.choices = chosen
    event:setCostData(self, cost_data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    if table.contains(cost_data.choices, 2) then
      room:addPlayerMark(player, "@@emo__chuoxu-turn")
      data.n = data.n + 2
    end
    if table.contains(cost_data.choices, 3) then
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
      if turn_event then
        local turn_data = turn_event.data
        for i = turn_data.phase_index + 1, #turn_data.phase_table do
          local current_data = turn_data.phase_table[i]
          if current_data.phase == Player.Discard then
            local discard = table.remove(turn_data.phase_table, i)
            table.insert(turn_data.phase_table, turn_data.phase_index + 1, discard)
            break
          end
        end
      end
      data.n = data.n + 3
    end
    if table.contains(cost_data.choices, 1) then
      data.n = data.n + 1
      local color, tos = cost_data.slash_data.color, cost_data.slash_data.tos
      local cards = table.filter(player:getCardIds("h"), function (id)
        return Fk:getCardById(id):getColorString() == color
      end)
      local slash = Fk:cloneCard("slash")
      slash.skillName = skel.name
      slash:addSubcards(cards)
      room:useCard{from = player, tos = tos, card = slash, extraUse = true}
    end
  end,
})

skel:addEffect("maxcards", {
  fixed_func = function (self, player)
    if player:hasSkill(skel.name) and player:getMark("@@emo__chuoxu-turn") ~= 0 then
      return 2
    end
  end,
})

local skel_vs = fk.CreateSkill {
  name = "emo__chuoxu_viewas",
}

skel_vs:addEffect("viewas", {
  interaction = function (self, player)
    local choices = {}
    for _, id in ipairs(player:getCardIds("h")) do
      table.insertIfNeed(choices, Fk:getCardById(id):getColorString())
      if #choices > 2 then break end
    end
    return UI.ComboBox{ choices = choices }
  end,
  card_filter = Util.FalseFunc,
  view_as = function (self, player)
    local color = self.interaction.data
    local cards = table.filter(player:getCardIds("h"), function (id)
      return Fk:getCardById(id):getColorString() == color
    end)
    if #cards > 0 then
      local card = Fk:cloneCard("slash")
      card.skillName = skel.name
      card:addSubcards(cards)
      return card
    end
  end,
})


return {skel, skel_vs}
